package configLoad

import (
	"github.com/gogf/gf/frame/g"
	"strconv"
	"strings"
)

type ProjectInfo struct {
	ID         int
	Name       string
	Port       int
	PrefixPath string
	ServerRoot string
	LogPath    string
}

var appProjectArray = make([]ProjectInfo, 0)
var launchIDs = make([]string, 0)

func GetAppProjectArray() []ProjectInfo {
	return appProjectArray
}

func GetLaunchIDs() []string {
	return launchIDs
}

func GetAppProjectByID(projectID string) *ProjectInfo {
	for _, item := range appProjectArray {
		if strconv.Itoa(item.ID) == projectID {
			return &item
		}
	}
	return nil
}

func LoadProjects() {
	appConfig := g.Config()
	sJson := appConfig.GetJson("app")
	launchAppArray := sJson.GetString("appIDs")
	projectsArray := sJson.GetJsons("projects")
	//解析启动的id
	var IDs = strings.Split(launchAppArray, ",")
	for _, id := range IDs {
		launchIDs = append(launchIDs, id)
	}
	//解析项目信息
	for _, item := range projectsArray {
		info := ProjectInfo{}
		info.ID = item.GetInt("id")
		info.Name = item.GetString("name")
		info.Port = item.GetInt("port")
		info.PrefixPath = item.GetString("prefix_path")
		info.ServerRoot = item.GetString("ServerRoot")
		info.LogPath = item.GetString("log_path")
		appProjectArray = append(appProjectArray, info)
	}
}
